<?php

namespace app\api\controller\user;

use app\common\controller\Api;
use think\Db;


class Collect extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 收藏列表
     * @ApiMethod (GET)
     * @path api/user/collect/index
     */
    public function index()
    {
        $page = $this->request->param('page',1);
        $storeId = $this->auth->store_id;
        $userId = $this->auth->id;
        $collect = db('user_collect')
            ->alias('a')
            ->join('goods b','a.goods_id = b.id','left')
            ->where(['a.user_id'=>$userId,'a.user_del'=>'0','a.store_id'=>$storeId])
            ->paginate(10, false, ['page' => $page]);
        $collect = $collect->toArray();
        $collect['data'] = addWebSiteUrl($collect['data'],['image','goods_images']);
        $collect['data'] = goodsPriceReset($collect['data'],$this->auth->type);

        $backList = [
            'list'=>$collect
        ];
        $this->success('操作成功',$backList);
    }

    /**
     * 删除收藏
     * @ApiMethod (GET)
     * @path api/user/collect/delete
     */
    public function delete()
    {
        $storeId = $this->auth->store_id;
        $userId = $this->auth->id;
        $id = $this->request->post('id');
        $where = [];
        if ($id > 0) {
            $where['id'] = $id;
        }
        db('user_collect')->where(['user_id'=>$userId])->where($where)->delete();
        $this->success('删除成功');
    }
}
